package cn.pigeon.usermanager.service.impl;

import cn.pigeon.usermanager.mapper.UserMapper;
import cn.pigeon.usermanager.pojo.User;
import cn.pigeon.usermanager.pojo.UserDetail;
import cn.pigeon.usermanager.pojo.UserView;
import cn.pigeon.usermanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名和密码查询用户
     * @param user
     * @return
     */
    public User findByNameAndPassword(User user) {
        return userMapper.findByNameAndPassword(user);
    }

    /**
     * 查询所有用户信息,关联用户详情表
     * @return
     */
    public List<UserView> findAll(UserView userView) {
        String username = userView.getUsername();
        String address = userView.getAddress();
        String qq = userView.getQq();
        if(username!=null){
            userView.setUsername("%"+username+"%");
        }
        if(address!=null){
            userView.setAddress("%"+address+"%");
        }
        if(qq!=null){
            userView.setQq("%"+qq+"%");
        }
        return userMapper.findAll(userView);
    }

    /**
     * 根据用户id查询用户信息(关联用户详情)
     * @param userId
     * @return
     */
    public User findByid(int userId) {
        return userMapper.findByid(userId);
    }

    /**
     * \根据用户id查询用户详情
     * @param userId
     * @return
     */
    public UserDetail findDetailByUserId(int userId) {
        return userMapper.findDetailByUserId(userId);
    }

    /**
     * 根据用户详情查询用户信息
     * @param userDetail
     * @return
     */
    public List<User> findByDetail(UserDetail userDetail) {
        return userMapper.findByDetail(userDetail);
    }

    /**
     * 根据id删除用户
     * @param id
     */
    @Transactional
    public void deleteById(int id) {
        userMapper.deleteDetailByUserId(id);
        userMapper.deleteById(id);
    }

    /**
     * 添加用户信息
     * @param user
     */
    @Transactional
    public void add(UserView userView) {
        //添加用户
        User user = new User();
        user.setUsername(userView.getUsername());
        user.setPassword(userView.getPassword());
        userMapper.add(user);

        //添加用户详情
        UserDetail detail = new UserDetail();
        detail.setAddress(userView.getAddress());
        detail.setQq(userView.getQq());
        detail.setUserId(user.getId());
        //传入添加后得到的用户自增长id
        userMapper.addDetail(detail);
    }

    /**
     * 更新用户信息
     * @param user
     */
    @Transactional
    public void update(UserView userView) {
        User user = new User();
        user.setId(userView.getId());
        user.setUsername(userView.getUsername());
        user.setPassword(userView.getPassword());
        userMapper.update(user);

        UserDetail detail = new UserDetail();
        detail.setAddress(userView.getAddress());
        detail.setQq(userView.getQq());
        detail.setId(userView.getDid());
        userMapper.updateDetail(detail);
    }

    public void updateDetail(UserDetail userDetail, int userId) {

    }
}
